<template>
  <!-- 步骤条模块 -->
  <div class="tools">
    <div class="step">
      <div
        v-for="(item, i) in steps.lists"
        :key="i"
        class="step-item"
        :class="{
          active: steps.value >= i
        }"
        :style="{
          right: `${i * 40}px`,
          zIndex: steps.lists.length - i
        }"
      >
        <p>{{ item }}</p>
        <el-icon class="title-icon">
          <CircleCheckFilled v-show="steps.value >= i" />
        </el-icon>
      </div>
    </div>
    <div class="edit">
      <el-button-group size="default">
        <el-button type="info" @click="emit('back')">
          取消
        </el-button>
        <el-button @click="prev" type="info" :disabled="steps.value === 0">
          上一步
        </el-button>
        <el-button @click="next" type="primary">
          {{ steps.value === steps.lists.length - 1 ? '保存' : '下一步' }}
        </el-button>
      </el-button-group>
    </div>
  </div>
  <div class="tabs-content">
    <div class="tab-pane" v-if="steps.value === 0">
      <div class="content-title">
        基本信息登记
      </div>
      <el-form
        class="base-form"
        ref="form"
        :model="formData"
        :rules="formDataRules"
        label-width="120px"
      >
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入模板名称" />
        </el-form-item>
        <!-- <el-form-item label="模板状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio-button :label="1">
              启用
            </el-radio-button>
            <el-radio-button :label="0">
              禁用
            </el-radio-button>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="创建人" prop="createdBy" v-if="formData.createdBy">
          <el-input v-model="formData.createdBy" placeholder="请输入创建人" readonly />
        </el-form-item>
      </el-form>
    </div>
    <div class="tab-pane" v-if="steps.value === 1">
      <Drag />
    </div>
    <div class="tab-pane" v-if="steps.value === 2">
      <Preview :main-list="mainList" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue'
import Drag from './drag.vue'
import Preview from './preview.vue'
import { addConfig, editConfig } from '@/api/system'
import { uploadFile } from '@/api/upload'
import { ElMessage } from 'element-plus'
import html2canvas from 'html2canvas'
import { getFormData, pressImg } from './composables'

const emit = defineEmits(['back'])
const steps = reactive({
  value: 0,
  lists: ['模板基本信息登记', '配置组件', '模板预览']
})

const prev = () => {
  steps.value -= 1
}
const next = async () => {
  switch (steps.value) {
    // 模板基本信息登记
    case 0: {
      const valid = await form.value?.validate()
      if (!valid) return
      break
    }
    // 配置组件
    case 1: {
      if (!formData.value.mainList.length) {
        return ElMessage.error('至少分配一个模块')
      }
      const dom = document.querySelector('.view-content-container') as HTMLElement
      const canvas = await html2canvas(dom, {
        useCORS: true
      })
      canvas.toBlob(async blob => {
        if (!blob) return ElMessage.error('图片转换失败')
        const file = new File([blob], new Date().getTime() + '.png', { type: 'image/png' })
        pressFile.value = await pressImg({
          file,
          width: dom.clientWidth * 0.5,
          height: dom.clientHeight * 0.5
        })
        steps.value += 1
      })
      return
    }
    // 预览保存
    case 2: {
      const params = {
        id: formData.value.id as any,
        name: formData.value.name,
        status: formData.value.status,
        createdBy: formData.value.createdBy,
        imageUrl: '',
        mainJson: JSON.stringify(formData.value.mainList)
      }
      const data = await uploadFile({ file: pressFile.value, type: 'avatar' })
      params.imageUrl = data
      if (params.id) {
        await editConfig(params)
        ElMessage.success('编辑成功')
      } else {
        delete params.id
        await addConfig(params)
        ElMessage.success('添加成功')
      }
      emit('back')
      return
    }
  }
  steps.value += 1
}

// 表单数据
const form = ref()
const formData = getFormData()
const pressFile = ref()
const formDataRules = ref({
  name: [{ required: true, message: '请输入模板名称', trigger: 'blur' }]
})

// 模板数据
const drag = ref()
const mainList = computed(() => drag.value ? drag.value.mainList : [])

</script>

<script lang="ts">
export default {
  name: 'Step'
}
</script>
<style lang="scss" scoped>
.tools {
  width: 100%;
  font-size: 16px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  margin-bottom: $space-margin;
  // 步骤条
  .step {
    height: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    .step-item {
      flex: 0 0 280px;
      height: 100%;
      display: flex;
      cursor: pointer;
      justify-content: center;
      align-items: center;
      color: rgba(130, 145, 169, 0.75);
      background: url('@/assets/temp/setp-bg.png') 0 0 no-repeat;
      background-size: 100% 100%;
      position: relative;
      &.active {
        color: #fff;
        background: url('@/assets/temp/setp-bg-active.png') 0 0 no-repeat;
        background-size: 100% 100%;
      }
      .title-icon {
        margin-left: 6px;
        width: 16px;
      }
    }
  }
  // 操作栏
  .edit {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}
.base-form {
  width: 50%;
  :deep(.el-form-item) {
    .el-form-item__label {
      color: #fff;
    }
    .el-input {
      --el-input-bg-color: rgba(#161D26, .7);
      --el-input-text-color: #8291A9;
      --el-input-border-color: rgba(#8291A9, .25);
    }
    .el-input__wrapper {
      border: 1px solid #8291A9;
      background-color: rgba($color: #161D26, $alpha: .7);
    }
  }
}
</style>
